// ==UserScript==
// @name           Yahoo Baseball Sort Sabermetrics Table
// @namespace      http://www.glenncarr.com/yahoofantasy
// @description    Allow sorting of Yahoo sabermetrics tables
// @include        http://sports.yahoo.com/fantasy/mlb/news*?slug=ys-sabertable_*
// @require        http://yahoofantasybrowsertools.googlecode.com/svn/trunk/TableSorter/src/sorttable.js
// $LastChangedRevision: 489 $
// $LastChangedDate: 2008-08-15 16:13:57 +0000 (Fri, 15 Aug 2008) $
// ==/UserScript==
(function()
{

var table = document.evaluate( "//table/tbody/tr/td/font/strong[.='Player']/../../../../..", document.body, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null );
if ( table.snapshotLength != 1 )
    return;
    
table = table.snapshotItem( 0 );

var iLastActivityCol = 0;
var iSortCol = -1;
var rows = new Array();

var class = table.getAttribute( "class" );
if ( class == null || class == "" )
    class = "sortable";
else
    class += " sortable";
table.setAttribute( "class", class );

var headerBackground = document.defaultView.getComputedStyle(table.rows[ 0 ].cells[ 0 ], '').getPropertyValue("color");
var headerColor = document.defaultView.getComputedStyle(table.rows[ 0 ].cells[ 0 ], '').getPropertyValue("background-color");

GM_addStyle( '\
table.sortable thead { cursor: pointer; } \
table.sortable th.sorttable_sorted_reverse, table.sortable th.sorttable_sorted { background: ' + headerBackground + '; color: ' + headerColor + ' } \
div#gncSortNote { font-size: 75%; font-style: italic; text-align: right; padding-bottom: 2px } \
' );

var headerFound = false;
var toRemove = new Array();
for ( var iRow = 0; iRow < table.rows.length; iRow++ )
{
    var row = table.rows[ iRow ];
    if ( !headerFound && /.*Player.*/.test( row.cells[ 0 ].innerHTML ) )
    {
    	headerFound = true;
        for ( var i = 0; i < row.cells.length; i++ )
        {
            row.addEventListener( 'click', function(e) {
                recolorRows( table );
            }, false );
        }
    }
    else if ( /.*Player.*/.test( row.cells[ 0 ].innerHTML ) || row.cells[ 0 ].innerHTML == '' )
    {
    	toRemove.push( row );
    }
}
alert( toRemove.length );
while ( toRemove.length > 0 )
{
	var row = toRemove.pop();
	row.parentNode.removeChild( row );
}

function recolorRows( table )
{
    for ( var iRow = 0; iRow < table.rows.length; iRow++ )
    {
        var row = table.rows[ iRow ];
        if ( /^td$/i.test( row.cells[ 0 ].tagName ) )
        {
            row.setAttribute( "class", ( iRow % 2 == 0 ) ? 'even' : 'odd' );
        }
    }
}

})();